<template>
  <div class="app-container">
    <el-form
      :model="formData"
      ref="formData"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="项目状态" prop="status">
        <el-select
          v-model="formData.status"
          filterable
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in statusList"
            :key="item.label"
            :label="item.label"
            :value="item.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目名称">
        <el-input
          v-model="formData.projectName"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!-- v-hasPermi="['customerService:ttsTestList:add']" -->
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="onClickAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain size="mini" @click="onEnable"
          >启用</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain size="mini" @click="onDisable"
          >禁用</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格   -->
    <el-table
      :loading="loading"
      :data="tableData"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="名称" align="center" prop="projectName" />
      <el-table-column
        label="业务类型"
        align="center"
        prop="businessType"
      ></el-table-column>
      <el-table-column label="外呼类型" align="center" prop="outboundType" />
      <el-table-column label="线路" align="center" prop="outboundLine" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" @click="onEdit(scope.row)" type="text"
            >修改</el-button
          >
          <el-button size="mini" @click="onAdvanced(scope.row)" type="text"
            >高级</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pagination.pageNum"
      :limit.sync="pagination.pageSize"
      @pagination="getList"
    />

    <!-- 新增 -->
    <add-project-dialog
      :add-project-dialog-show="addProjectDialogShow"
      :id="currentId"
      @onCloseAddProjectDialog="onCloseAddProjectDialog"
    ></add-project-dialog>

    <!-- 高级设置 -->
    <advanced-setting-dialog
      :advanced-setting-dialog-show="advancedSettingDialogShow"
      :id="currentId"
      @onAdvancedDialog="onAdvancedDialog"
    ></advanced-setting-dialog>
  </div>
</template>

<script>
import addProjectDialog from "./components/addProjectDialog.vue";
import advancedSettingDialog from "./components/advancedSettingDialog.vue";
import { projectList, updateStatus } from "@/api/projectManagement/list.js";
import { Message } from "element-ui";
export default {
  name: "projectManagementList",
  components: { addProjectDialog, advancedSettingDialog },
  data() {
    return {
      loading: false,
      tableData: [], //表格数据
      showSearch: true, //显示搜索条件
      // 查询参数
      formData: {
        status: "", //状态
        projectName: "", //名称
      },
      statusList: [
        { value: 1, label: "禁用" },
        { value: 0, label: "启用" },
      ], //项目状态
      pagination: {
        pageNum: 1,
        pageSize: 10,
      },
      idList: [],
      total: 0,
      addProjectDialogShow: false,
      advancedSettingDialogShow: false,
      currentId: "",
    };
  },
  async created() {
    this.getList();
  },
  methods: {
    // 列表查询
    async getList() {
      const params = {
        ...this.formData,
        ...this.pagination,
      };

      this.loading = true;
      const { total, rows } = await projectList(params);
      this.total = total;
      this.tableData = rows;
      this.loading = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.pagination.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.formData = {
        status: 0, //状态
        projectName: "", //名称
      };
      this.handleQuery();
    },

    // 启用
    onEnable() {
      this.toUpdateStatus("启用");
    },

    async toUpdateStatus(status) {
      if (!this.idList.length) {
        Message.warning("请选择数据！");
        return;
      }
      const params = {
        idList: this.idList,
        status,
      };
      const { code, msg } = await updateStatus(params);
      if (code === 200) {
        Message.success(msg);
      }
      this.getList();
    },

    // 禁用
    onDisable() {
      this.toUpdateStatus("禁用");
    },

    // 高级
     onAdvanced(row) {
      this.currentId = String(row.id);
      this.advancedSettingDialogShow = true;
    },

    // 高级弹窗关闭
    onAdvancedDialog(){
      this.currentId = '';
      this.advancedSettingDialogShow = false;
      this.getList();
    },

    // 新建
    onClickAdd() {
      this.addProjectDialogShow = true;
    },

    // 关闭
    onCloseAddProjectDialog(){
      this.addProjectDialogShow = false;
      this.currentId = ''
      this.getList();
    },

    // 编辑
    onEdit(row) {
      this.addProjectDialogShow = true;
      this.currentId = String(row.id);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.idList = selection.map((item) => item.id);
    },
  },
};
</script>

<style lang="scss" scoped></style>
